home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group96a.txt / 000073_icon-group-sender _Wed Apr 10 13:44:12 1996.msg < prev    next >
Internet Message Format  |  1996-09-05  |  4KB

  1. Received: by cheltenham.cs.arizona.edu; Wed, 10 Apr 1996 12:36:38 MST
  2. To: icon-group@cs.arizona.edu
  3. Date: 10 Apr 1996 13:44:12 GMT
  4. From: espie@bireme.ens.fr (Marc Espie)
  5. Message-Id: <4kgdvc$1v6@nef.ens.fr>
  6. Organization: Ecole Normale Superieure, Paris
  7. Sender: icon-group-request@cs.arizona.edu
  8. References: <4kgc8d$j7h@solutions.solon.com>
  9. Subject: Re: How to think about Icon?
  10. Errors-To: icon-group-errors@cs.arizona.edu
  11. Status: O
  12.  
  13. In article <4kgc8d$j7h@solutions.solon.com>,
  14. Peter Seebach <seebs@solon.com> wrote:
  15. >I've made periodic stabs at learning icon for some time now, and I've
  16. >come to the conclusion that it's completely unlike the languages I've
  17. >learned previously.  In particular, I have no good mental model for
  18. >backtracking, so I get confused by programs which do it.  This makes Icon
  19. >hard for me to take advantage of.  :)
  20. >
  21. >What kind of conceptual model do you use for programming in Icon?  How
  22. >do you use it differently from "simple" procedural languages, like C
  23. >or Perl?
  24. >
  25. I don't know how it works for other people, but `iterator' is the key for me.
  26. The classical problem that is difficult to work in other languages is
  27. graph traversal.
  28.  
  29. In C, you usually will code a given graph traversal algorithm that takes
  30. a call back function as a parameter---this call back will be called with each
  31. vertex you encounter. In Icon, you will design a generator that suspends every
  32. vertex you encounter. Up to the calling program to decide what to do with
  33. the vertex.
  34.  
  35. This goes farther than it seems. For instance, I usually don't hardcode
  36. my definition of a graph, but use a generator that gives me the neighbors
  37. of a given neighbor instead.
  38.  
  39. Sample Connected Component program (this is what I actually use in practice):
  40.  
  41. procedure build_cc(v, neighors, para, cc)
  42.     local todo
  43.  
  44.     todo := [v]
  45.     /cc := set()    # initialize set if needed
  46.     insert(cc, v)    # cc initially holds v
  47.     while v := pop(todo) do
  48.         every neighbor := neighbors(v, para) do
  49.             member(cc, neighbor) | (insert(cc, neighbor) & put(todo, neighbor))
  50.     return cc
  51. end
  52.  
  53. As far as backtracking goes, the sanest way is perhaps to just consider it
  54. as a kind of shorthand (at first): all the logical structure and all the
  55. C variables are there, you just don't have to write it down explicitly, i.e.,
  56. going from
  57.     every i:= 1 to 10 do
  58.         write(i)
  59. to 
  60.     every write(1 to 10)
  61. or
  62.     if (i = 0)  | (i = 1)
  63. to
  64.     if i = (0|1)
  65. isn't that difficult.
  66.  
  67. Then you can go farther to recursive generators and such intricate stuff.
  68. This is the same conceptual leap as recursive invariants in functional
  69. languages. The only thing you have to watch for is the precise semantics
  70. of failure/multiple success... If I counted all the times I had a generator
  71. resume on me when I wasn't expecting it, reverse the first result, and
  72. finally fail---\1 is very handy at times, or all these procedural functions
  73. that forgot to return anything and just failed, simply !
  74.  
  75. Another fun example: this one generates all permutations of a given list,
  76. and is quite nice to understand generator resumption.
  77.  
  78. procedure do_permute(l, i, n)
  79.     if i >= n then
  80.         return l
  81.     else
  82.         suspend l[i to n] <-> l[i] & do_permute(l, i+1, n)
  83. end
  84.  
  85. procedure permute(l)
  86.     suspend do_permute(l, 1, *l)
  87. end
  88.  
  89. I should add that, again, this is a real life example. Icon code tends to
  90. be very compact, elegant, and not so easy to understand at times :-)
  91.  
  92. If you haven't already, go out and buy the Icon programming language book,
  93. by Griswold. This is a very nice book, that gives lots of applications of
  94. the generator/backtracking paradigm.
  95.      
  96.  
  97.  
  98. -- 
  99. [nosave]<http://www.eleves.ens.fr:8080/home/espie/index.html>
  100. microsoft network is EXPLICITLY forbidden to redistribute this message.
  101. `Moon purismu powa, make up.... Tsuki ni kawatte, oshiokiyo !'
  102.     Marc Espie (Marc.Espie@ens.fr)
  103.